import { EMIT_PARAMS_OBJ } from '@/utils/constant';
import modelUtil from '@/utils/modelUtil.js';
import { isEmpty } from '@/utils/util';
export default {
  methods: {

    _updateParamsxMinerItem(obj, key) {
      const paramObj = obj[key];
      const paramKey = paramObj.key;
      const value = obj[paramKey];

      // 特殊情况 时间有可能有2个key
      const paramKey2 = paramObj.key_endTime;
      const value2 = obj[paramKey2];

      // value为空时不放入params
      const paramItem = {
        code: paramKey,
        value: value,
      };
      const paramIndex = this.params.params.findIndex(p => String(p.code) === String(paramKey));
      if (paramIndex === -1) {
        if (!isEmpty(value)) {
          this.params.params.push(paramItem);
        }
      } else {
        // 最后发出的事件条件放在所有参数最后面，保证使用最后的值作为查询条件
        this.params.params.splice(paramIndex, 1);
        if (!isEmpty(value)) {
          this.params.params.push(paramItem);
        }
      }

      // 特殊情况 时间有可能有2个key
      if (paramKey2) {
        const param2 = this.params.params.find(p => String(p.code) === String(paramKey2));
        if (param2) {
          param2.value = value2 ? value2 : param2.value;
        } else {
          if (value2) {
            this.params.params.push({
              code: paramKey2,
              value: value2,
            });
          }
        }
      } else {
        // 当且仅当时间筛选组件时间范围切换时间点场景才删除值
        const timeOption = paramObj.option;
        if (!timeOption) {
          return;
        }
        const endPram = this.params.params.find(p => String(p.code) === String(timeOption.key_endTime));
        if (endPram) {
          const index = this.params.params.indexOf(endPram);
          this.params.params.splice(index, 1);
        }
      }
    },

    updateParamsxMiner(obj, dataSourceType = 'xMiner') {
      // 删除已删发出组件的参数
      const { wid } = this.needClearOutEvent;
      if (this.params.params && wid) {
        this.params.params = this.params.params.filter(n => n.wid !== wid);
      }

      for (const key in obj) {
        if (obj[key] === undefined) {
          continue;
        }
        if (key === 'xTag_parms_type') {
          continue;
        }
        if (key.indexOf(EMIT_PARAMS_OBJ) <= -1) {
          continue;
        }
        if (!this.params.params) {
          this.params.params = [];
        }

        const hasEvent = this.config.events.find(n => n.type === 'in' && n.key === obj[key].eventKey);
        if (hasEvent) {
          this.addReceivedEvents(obj[key], 'normal');
        }

        // 参数值存放在EMIT_PARAMS_OBJ内部，不放在外部，实验性方案，触发参考slide-fitler组件
        const updateRes = this.updateParamsByWid({ [key]: obj[key] }, dataSourceType, this.params);
        if (updateRes) {
          continue;
        }

        const paramObj = obj[key];
        const { eventKey } = paramObj;

        // 遍历config 查看配置的事件
        this.config.events.forEach(event => {
          if (event.type === 'in' && event.key === eventKey) {
            this._updateParamsxMinerItem(obj, key); // 降低圈复杂度做函数分割
          }
        });
      }
    },

    hasEvent(evtKey) {
      return this.config.events.find(n => n.type === 'in' && n.key === evtKey);
    },

    // 修正参数
    reviseParamsxMiner(curParams) {
      return modelUtil.reviseParamsxMiner(curParams);
    },
    reviseParamsxMinerPublicDatabase(curParams) {
      const _params = JSON.parse(JSON.stringify(curParams));

      // 删除有可能出现的xtag参数（编排端切换数据源）
      delete _params.conditions;
      delete _params.sqlParams;

      const { params } = _params;
      if (params && params.length) {
        params.reverse();
        const dict = {}; // 记录出现过的key
        for (let ii = 0; ii < params.length; ii++) {
          const item = params[ii];
          if (item.wid) {
            if (dict[item.code]) {
              params.splice(ii, 1);
              ii--;
            } else {
              dict[item.code] = 1;
            }
          }
        }
        params.reverse();
      }
      return _params;
    },
  },
};
